# @Time : 2021/10/26 14:07
# @Author : Li Kunlun
# @Description : 进程池


import multiprocessing as mp


def job(x):
    return x * x


def multicore():
    # Pool默认调用是CPU的核数，传入processes参数可自定义CPU核数
    pool = mp.Pool(processes=2)
    # map() 放入迭代参数，返回多个结果
    res = pool.map(job, range(10))
    print(res)

    # apply_async()只能放入一组参数，并返回一个结果
    res = pool.apply_async(job, (2,))
    print(res.get())

    # 如果想得到map()的效果apply_async需要通过迭代
    multi_res = [pool.apply_async(job, (i,)) for i in range(10)]
    print([res.get() for res in multi_res])


if __name__ == '__main__':
    multicore()
